﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TfsBugger.Buggers;
using TfsBugger.Core;
using TfsBugger.Util.Ui;

namespace TfsBugger.Forms
{
    /// <summary>
    /// Interaction logic for CheckInBuggerWindow.xaml
    /// </summary>
    public partial class CheckInBuggerWindow : Window
    {
        public CheckInBuggerWindow()
        {
            InitializeComponent();
        }


        #region Properties
        private Storyboard SpinnerStoryBd
        {
            get { return (Storyboard)rectSpinner.Resources["ProgressAnimation"]; }
        }


        private CheckInBugger CheckInBugger
        {
            get { return (CheckInBugger)Resources["CheckInBugger"]; }
        }


        private bool ClosePending { get; set; }
        #endregion


        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            this.cboProjects.ItemsSource = from a in App.SelectedProjects 
                                           orderby a.Name ascending 
                                           select a.Name;
        }


        private void OnSelectedProjChanged(object sender, SelectionChangedEventArgs e)
        {
            this.btnStart.IsEnabled = null != this.cboProjects.SelectedItem;
        }


        private void OnStartButtonClick(object sender, RoutedEventArgs e)
        {
            rectSpinner.Visibility = Visibility.Visible;
            lblBugStatus.Visibility = Visibility.Visible;
            SpinnerStoryBd.Begin(this, true);

            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;

            //Init the bugger and go 
            CheckInBugger.RunAsync(App.TfsServer, new CheckInBuggerArgs { ProjectName = this.cboProjects.Text });
        }


        private void OnStopButtonClick(object sender, RoutedEventArgs e)
        {
            btnStop.IsEnabled = false;
            btnStart.IsEnabled = false;
            lblBugStatus.Text = "Stopping...";
            CheckInBugger.CancelAsync();
        }


        private void OnPollingStatusUpdated(object sender, PollingStatusUpdatedEventArgs e)
        {
            lblBugStatus.Text = e.Status;
        }


        private void OnCheckInTargetUpdated(object sender, TargetUpdatedEventArgs e)
        {
            var state = (CheckInBuggerState)e.State;
            lstCheckIns.Items.Add(state.LatestChangeset);
        }


        private void OnBuggerWorkFinished(object sender, BuggerWorkFinishedEventArgs e)
        {
            btnStop.IsEnabled = false;
            btnStart.IsEnabled = true;

            SpinnerStoryBd.Stop(this);
            rectSpinner.Visibility = Visibility.Hidden;
            lblBugStatus.Visibility = Visibility.Hidden;
            if (ClosePending)
                Close();
        }

        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            DoClose(true);
        }


        private void OnWindowClosing(object sender, CancelEventArgs e)
        {
            DoClose(false);
        }


        #region Methods
        private void DoClose(bool close)
        {
            CheckInBugger.CancelAsync();
            using (UiWaitScope wait = new UiWaitScope(this))
            {
                while (CheckInBugger.IsBusy)
                    Thread.Sleep(1000);
            }

            if (close)
                Close();
        }
        #endregion
    }
}
